<?php

require_once 'MessageCode.php';

class RoomDetail
{

    public $roomId;//房间id
    public $playerId;//玩家id
    public $roomStatus; //1游戏中,0空闲
    public $playerList = [];//当前玩家列表
    public $playerVote = [];//玩家投票的二位数组,key为玩家ID ,投票状态 0 未投票[1000=>0] 1同意[1001=>1] 2拒绝[1002=>2]
    public $playerReady = [];//玩家准备，key为玩家ID ,准备状态  1准备[1001=>1] 2未准备[1002=>2]
    public $roomCard = [];//房间当前手牌
    public $roomUsedCard;//已经用过的手牌


    public function __construct($roomId)
    {
        $this->roomId = $roomId;
        $this->roomStatus = 0;

    }


    /**
     * chenming 2018-07-13
     * 提示玩家加入房间
     * @return string
     */
    public function playerJoin($playerId, $playerDetail)
    {
        if ($this->roomStatus == 1) {
            return MessageCode::$STATUS_START;
        }
        if (count($this->playerList) == 3) {
            return MessageCode::$STATUS_FULL;
        }
        $this->playerList[$playerId] = $playerDetail;
        $this->playerReady[$playerId] = [1002 => 2];
        return MessageCode::$STATUS_JOIN_SUCCESS;
    }

    /**
     * chenming 2018-07-13
     * 玩家离开房间
     * @param $playerId
     */
    public function playerOut($playerId)
    {

        unset($this->playerList[$playerId]);
        $this->roomStatus = 0;
        return MessageCode::$STATUS_OUT_SUCCESS;
    }

    /**
     * chenming 2018-07-13
     * 游戏开始
     * @param $playerId
     */
    public function playerReady($playerId, $readyDetail = [1002 => 2])
    {
        $this->playerReady[$playerId] = $readyDetail;
        $status = array_column($this->playerReady, '1001');
        if (count($this->playerList) == 3) {
            if (count($status) == 3) {
                $this->roomStatus = 1;
                return MessageCode::$STATUS_GAME_START;
            } else {
                return MessageCode::$STATUS_VOTE_FAIL;
            }
        } else {
            return MessageCode::$STATUS_NO_FULL;
        }


    }

    /**
     * chenming 2018-07-13
     * 房间玩家列表
     * @param $roomId
     * @return string
     */
    public function playerList()
    {
        return $this->playerList;

    }

    /**
     * chenming 2018-07-13
     * 房间发起投票
     */
    public function playerVote($playerId, $voteDetail = [1000 => 0])
    {
        //判断是否投过票
        if (isset($this->playerVote[$playerId])) {
            return MessageCode::$STATUS_VOTE;
        }
        $this->playerVote[$playerId] = $voteDetail;
        $status = array_column($this->playerVote, '1001');
        if (count($status) >= 2) {
            //投票通过，游戏解散
            $this->roomStatus = 0;
            return MessageCode::$STATUS_VOTE_SUCCESS;

        } else {
            return MessageCode::$STATUS_VOTE_FAIL;
        }
    }

    /**
     * chenming 2018-07-13
     * 房间玩家当前手牌
     * @param $playerId
     * @param $roomId
     * @return string
     */
    public function CurrentCard($playerId, $cardDetail)
    {
        $this->roomCard[$playerId] = $cardDetail;
        return $this->roomCard;
    }

    /**
     * chenming 2018-07-13
     * 房间已经用过的手牌
     * @param $playerId
     * @param $roomId
     * @return string
     */
    public function usedCard($cardDetail)
    {
        return $this->roomUsedCard = $cardDetail;
    }
}